package com.zerox.controller;

import com.alibaba.fastjson.JSON;
import com.zerox.domain.TableVo;
import com.zerox.pojo.SysUser;
import com.zerox.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class LoginController {
    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/login")
    public String login(){
        return "login";
    }
    @PostMapping("/tologin")
    public String login(Model model, HttpServletResponse response, String login_name, String password){
        SysUser sysUser = sysUserService.login(login_name,password);
        System.out.println(sysUser.getPassword());
        if (sysUser!=null&&sysUser.getPassword().equals(password)){
            return "Shouye";
        }
        return "login";
    }

    @GetMapping("/Shouye")
    public String Shouye(){
        return "Shouye";
    }

    @GetMapping("/SelectYongHu")
    @ResponseBody
    public void SelectYongHu(Model model,HttpServletResponse response,Integer page,Integer limit) throws IOException {
        List<SysUser> sysUserList = sysUserService.list((page - 1) * limit,limit);
        TableVo tableVo = new TableVo(0,2,sysUserList,"");
        String json = JSON.toJSONString(tableVo);
        response.getWriter().print(json);
    }

    @GetMapping("toUser")
    public String toUser(){
        return "YongHu";
    }
}